<script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n'
import { ElTag, ElButton, ElImage } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { getViewApi, delAdverApi } from '@/api/lunbotu'
import { AdverData } from '@/api/lunbotu/types'
import { TableColumn } from '@/types/table'
import { useValidator } from '@/hooks/web/useValidator'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { h, ref, unref, reactive } from 'vue'
import { FormSchema } from '@/types/form'
import { Table } from '@/components/Table'
import { useTable } from '@/views/Advertisement/useTable'

const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'display',
    label: '是/否显示',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '显示',
          value: '1'
        },
        {
          label: '不显示',
          value: '2'
        }
      ]
    }
  },
  {
    field: 'imgType',
    label: '图片类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '商品',
          value: '1'
        },
        {
          label: '视频',
          value: '2'
        },
        {
          label: '其他',
          value: '3'
        }
      ]
    }
  }
])

const columns = reactive<TableColumn[]>([
  {
    field: 'carousel_id',
    label: '用户ID',
  },
  {
    field: 'img_address',
    label: '图片地址',
  },
  {
    field: 'create_user',
    label: '创建人'
  },
  {
    field: 'img_title',
    label: '图片标题'
  },
  {
    field: 'img_type',
    label: '图片类型'
  },
  {
    field: 'remarks',
    label: '备注'
  },
  {
    field: 'update_date',
    label: '更新时间',
    table: {
      show: true
    }
  },
  {
    field: 'action',
    width: "250px",
    label: t('tableDemo.action')
  }
])

//获取轮播图数据
const { register, tableObject, methods } = useTable<AdverData>({
  getListApi: getViewApi,
  delListApi: delAdverApi,
  response: {
    list: 'list',
    total: 'total'
  },
  props: {
    columns
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { push } = useRouter()

const AddAction = () => {
  push('/rotograph/advertisementAdd')
}

const delLoading = ref(false)

const delData = async (row: AdverData | null, multiple: boolean) => {
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.carousel_id) : [tableObject.currentRow?.carousel_id as number],
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}
//.slice((tableObject.currentPage-1)*tableObject.pageSize,tableObject.currentPage*tableObject.pageSize)

const action = (row: AdverData, type: string) => {
  push(`/rotograph/advertisement-${type}?carousel_id=${row.carousel_id}`)
}
</script>

<template>
  <ContentWrap :title="t('searchDemo.search')">
    <Search :schema="schema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>
  <ContentWrap :title="`轮播图列表`">
    <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
    <Table
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :data="
        tableObject.tableList.slice(
          (tableObject.currentPage - 1) * tableObject.pageSize,
          tableObject.currentPage * tableObject.pageSize
        )
      "
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="primary" @click="action(row, 'edit')">
          {{ t('exampleDemo.edit') }}
        </ElButton>
        <ElButton type="success" @click="action(row, 'detail')">
          {{ t('exampleDemo.detail') }}
        </ElButton>
        <ElButton type="danger" @click="delData(row, false)">
          {{ t('exampleDemo.del') }}
        </ElButton>
      </template>
      <template #img_address="{ row }: { row: AdverData }">
          <ElImage
            style="width: 60px;height: 60px;"
            :src="row.img_address"
            :preview-src-list="[row.img_address]"
            hide-on-click-modal="true"
            preview-teleported="true"
        />
      </template>
    </Table>
  </ContentWrap>
</template>
